<template>
  <UserLayout :active-index="'3'" :active-menu="'agent-houses'">
    <div class="agent-houses-content">
      <!-- 未实名认证的提示 -->
      <div v-if="!isIdentityVerified" class="no-access-container">
        <div class="no-access-card">
          <div class="no-access-icon">
            <el-icon><User /></el-icon>
          </div>
          <h2>需要先完成实名认证</h2>
          <p>申请中介需要先完成实名认证，请先前往个人资料页面完成实名认证</p>
          <div class="no-access-actions">
            <el-button type="primary" @click="goToIdentityVerification">
              <el-icon><Plus /></el-icon>
              前往实名认证
            </el-button>
          </div>
          <div class="no-access-tips">
            <h3>实名认证流程：</h3>
            <ol>
              <li>填写真实姓名和身份证号</li>
              <li>系统验证身份信息</li>
              <li>认证通过后可申请中介</li>
            </ol>
          </div>
        </div>
      </div>

      <!-- 未申请中介的提示 -->
      <div v-else-if="agentStatus === 'no_application'" class="no-access-container">
        <div class="no-access-card">
          <div class="no-access-icon">
            <el-icon><User /></el-icon>
          </div>
          <h2>您还未申请中介</h2>
          <p>要查看可代理房源，您需要先申请成为中介</p>
          <div class="no-access-actions">
            <el-button type="primary" @click="goToAgentApply">
              <el-icon><Plus /></el-icon>
              申请中介
            </el-button>
          </div>
          <div class="no-access-tips">
            <h3>申请流程：</h3>
            <ol>
              <li>填写中介申请信息</li>
              <li>上传相关资质证明</li>
              <li>等待管理员审核</li>
              <li>审核通过后即可查看可代理房源</li>
            </ol>
          </div>
        </div>
      </div>

      <!-- 中介申请审核中的提示 -->
      <div v-else-if="agentStatus === 'pending'" class="no-access-container">
        <div class="no-access-card">
          <div class="no-access-icon status-pending">
            <el-icon><Clock /></el-icon>
          </div>
          <h2>中介申请审核中</h2>
          <p>您的中介申请正在审核中，请耐心等待管理员审核</p>
          <div class="no-access-actions">
            <el-button @click="goToAgentStatus">
              <el-icon><View /></el-icon>
              查看申请状态
            </el-button>
            <el-button @click="refreshStatus">
              <el-icon><Refresh /></el-icon>
              刷新状态
            </el-button>
          </div>
          <div class="no-access-tips">
            <h3>审核说明：</h3>
            <ol>
              <li>管理员将在7个工作日内完成审核</li>
              <li>审核期间请保持联系方式畅通</li>
              <li>如有疑问可联系客服</li>
              <li>审核通过后即可查看可代理房源</li>
            </ol>
          </div>
        </div>
      </div>

      <!-- 中介申请被拒绝的提示 -->
      <div v-else-if="agentStatus === 'rejected'" class="no-access-container">
        <div class="no-access-card">
          <div class="no-access-icon status-rejected">
            <el-icon><Close /></el-icon>
          </div>
          <h2>中介申请被拒绝</h2>
          <p>很抱歉，您的中介申请未通过审核，请查看拒绝原因并重新申请</p>
          <div class="no-access-actions">
            <el-button type="primary" @click="goToAgentApply">
              <el-icon><Plus /></el-icon>
              重新申请
            </el-button>
            <el-button @click="goToAgentStatus">
              <el-icon><View /></el-icon>
              查看详情
            </el-button>
          </div>
          <div class="no-access-tips">
            <h3>重新申请建议：</h3>
            <ol>
              <li>仔细查看拒绝原因</li>
              <li>完善相关资质材料</li>
              <li>确保信息真实有效</li>
              <li>重新提交申请</li>
            </ol>
          </div>
        </div>
      </div>

      <!-- 已通过中介申请的内容 -->
      <div v-else>
        <div class="houses-header">
          <h2>可代理房源</h2>
          <p>您可以申请代理以下房源，代理成功后可进行推广</p>
        </div>

          <!-- 房源统计数据 -->
          <div class="houses-stats">
            <el-row :gutter="20">
              <el-col :span="6">
                <div class="stat-card">
                  <div class="stat-icon bg-blue">
                    <House />
                  </div>
                  <div class="stat-content">
                    <div class="stat-value">{{ totalHouses }}</div>
                    <div class="stat-title">房源总数</div>
                  </div>
                </div>
              </el-col>
              <el-col :span="6">
                <div class="stat-card">
                  <div class="stat-icon bg-green">
                    <OfficeBuilding />
                  </div>
                  <div class="stat-content">
                    <div class="stat-value">{{ agentHouses }}</div>
                    <div class="stat-title">已代理</div>
                  </div>
                </div>
              </el-col>
              <el-col :span="6">
                <div class="stat-card">
                  <div class="stat-icon bg-orange">
                    <Star />
                  </div>
                  <div class="stat-content">
                    <div class="stat-value">{{ favoriteHouses }}</div>
                    <div class="stat-title">收藏房源</div>
                  </div>
                </div>
              </el-col>
              <el-col :span="6">
                <div class="stat-card" @click="goToAllApplications">
                  <div class="stat-icon bg-red">
                    <ChatLineSquare />
                  </div>
                  <div class="stat-content">
                    <div class="stat-value">{{ applicationCount }}</div>
                    <div class="stat-title">房源申请代理</div>
                  </div>
                </div>
              </el-col>
            </el-row>
          </div>

          <!-- 房源列表 -->
          <div class="houses-list">
            <el-tabs v-model="activeTab" @tab-change="handleTabChange">
              <el-tab-pane label="全部房源" name="all">
                <div class="houses-container">
                  <el-row :gutter="20">
                    <el-col
                        :span="8"
                        v-for="house in houseList"
                        :key="house.id"
                        class="house-col"
                    >
                      <div class="house-card" @click="viewHouseDetail(house.id)">
                        <div class="house-image">
                          <img :src="house.image" :alt="house.title" />
                          <div class="house-price">¥{{ house.price }}/月</div>
                          <div
                              class="favorite-button"
                              :class="{ favorited: house.favorited }"
                              @click.stop="toggleFavorite(house)"
                          >
                            <el-icon>
                              <Star v-if="house.favorited" />
                              <StarFilled v-else />
                            </el-icon>
                          </div>
                        </div>
                        <div class="house-info">
                          <h3 class="house-title">{{ house.title }}</h3>
                          <p class="house-address">
                            <el-icon><Location /></el-icon>
                            {{ house.address }}
                          </p>
                          <div class="house-meta">
                            <span>{{ house.layout }}</span>
                            <span>{{ house.area }}㎡</span>
                            <span>{{ house.orientation }}</span>
                          </div>
                          <div class="house-tags">
                            <el-tag
                                v-for="tag in house.tags"
                                :key="tag"
                                size="small"
                                :type="getTagType(tag)"
                            >
                              {{ tag }}
                            </el-tag>
                          </div>
                          <div class="house-actions">
                            <el-button
                                size="small"
                                type="primary"
                                @click.stop="applyForAgency(house)"
                            >
                              申请代理
                            </el-button>
                            <el-button
                                size="small"
                                @click.stop="contactLandlord(house)"
                            >
                              联系房东
                            </el-button>
                          </div>
                        </div>
                      </div>
                    </el-col>
                  </el-row>
                </div>
              </el-tab-pane>
              <el-tab-pane label="已收藏" name="favorited">
                <div class="houses-container">
                  <el-row :gutter="20">
                    <el-col
                        :span="8"
                        v-for="house in favoritedHouses"
                        :key="house.id"
                        class="house-col"
                    >
                      <div class="house-card" @click="viewHouseDetail(house.id)">
                        <div class="house-image">
                          <img :src="house.image" :alt="house.title" />
                          <div class="house-price">¥{{ house.price }}/月</div>
                          <div
                              class="favorite-button"
                              :class="{ favorited: house.favorited }"
                              @click.stop="toggleFavorite(house)"
                          >
                            <el-icon>
                              <Star v-if="house.favorited" />
                              <StarFilled v-else />
                            </el-icon>
                          </div>
                        </div>
                        <div class="house-info">
                          <h3 class="house-title">{{ house.title }}</h3>
                          <p class="house-address">
                            <el-icon><Location /></el-icon>
                            {{ house.address }}
                          </p>
                          <div class="house-meta">
                            <span>{{ house.layout }}</span>
                            <span>{{ house.area }}㎡</span>
                            <span>{{ house.orientation }}</span>
                          </div>
                          <div class="house-tags">
                            <el-tag
                                v-for="tag in house.tags"
                                :key="tag"
                                size="small"
                                :type="getTagType(tag)"
                            >
                              {{ tag }}
                            </el-tag>
                          </div>
                          <div class="house-actions">
                            <el-button
                                size="small"
                                type="primary"
                                @click.stop="applyForAgency(house)"
                            >
                              申请代理
                            </el-button>
                            <el-button
                                size="small"
                                @click.stop="contactLandlord(house)"
                            >
                              联系房东
                            </el-button>
                          </div>
                        </div>
                      </div>
                    </el-col>
                  </el-row>
                </div>
              </el-tab-pane>
              <el-tab-pane label="已代理" name="agency">
                <div class="houses-container">
                  <el-row :gutter="20">
                    <el-col
                        :span="8"
                        v-for="house in agencyHouses"
                        :key="house.id"
                        class="house-col"
                    >
                      <div class="house-card" @click="viewHouseDetail(house.id)">
                        <div class="house-image">
                          <img :src="house.image" :alt="house.title" />
                          <div class="house-price">¥{{ house.price }}/月</div>
                          <div class="agency-badge">已代理</div>
                        </div>
                        <div class="house-info">
                          <h3 class="house-title">{{ house.title }}</h3>
                          <p class="house-address">
                            <el-icon><Location /></el-icon>
                            {{ house.address }}
                          </p>
                          <div class="house-meta">
                            <span>{{ house.layout }}</span>
                            <span>{{ house.area }}㎡</span>
                            <span>{{ house.orientation }}</span>
                          </div>
                          <div class="house-tags">
                            <el-tag
                                v-for="tag in house.tags"
                                :key="tag"
                                size="small"
                                :type="getTagType(tag)"
                            >
                              {{ tag }}
                            </el-tag>
                          </div>
                          <div class="house-actions">
                            <el-button
                                size="small"
                                type="success"
                                @click.stop="viewAgencyDetails(house)"
                            >
                              代理详情
                            </el-button>
                            <el-button
                                size="small"
                                @click.stop="manageHouse(house)"
                            >
                              管理房源
                            </el-button>
                          </div>
                        </div>
                      </div>
                    </el-col>
                  </el-row>
                </div>
              </el-tab-pane>
            </el-tabs>
          </div>

          <!-- 分页 -->
          <div class="pagination-container">
            <el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="pageSize"
                :page-sizes="[9, 18, 36, 72]"
                :total="totalHouses"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </div>
  </UserLayout>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import UserLayout from '@/components/layout/UserLayout.vue'
import {
  House,
  Location,
  Star,
  StarFilled,
  User,
  Plus,
  View,
  Clock,
  Close,
  Refresh
} from '@element-plus/icons-vue'
import api from '@/utils/api'
import { getToken, getUserInfo } from '@/utils/auth'

const router = useRouter()
const activeMenu = ref('agent-houses')
const activeIndex = ref('1')
const activeTab = ref('all')
const searchKeyword = ref('')
const selectedCity = ref('')
const selectedPriceRange = ref('')
const selectedLayout = ref('')
const currentPage = ref(1)
const pageSize = ref(9)

// 用户状态
const isIdentityVerified = ref(false)
const agentStatus = ref('no_application') // 'no_application', 'pending', 'approved', 'rejected'
const hasAgentAccess = ref(false)
const loading = ref(true)

// 检查用户状态
const checkAgentStatus = async () => {
  try {
    loading.value = true

    // 首先检查用户是否已实名认证
    const userInfo = getUserInfo()
    if (!userInfo || !userInfo.isIdentityVerified) {
      isIdentityVerified.value = false
      hasAgentAccess.value = false
      return
    }

    isIdentityVerified.value = true

    // 检查中介申请状态
    const response = await api.get('/agent/auth/result', {
      headers: {
        'info': getToken()
      }
    })

    if (response.data.code === 200 && response.data.data) {
      const data = response.data.data
      // 根据verifyStatus设置状态
      switch (data.verifyStatus) {
        case 1:
          agentStatus.value = 'pending'
          hasAgentAccess.value = false
          break
        case 2:
          agentStatus.value = 'approved'
          hasAgentAccess.value = true
          break
        case 0:
          agentStatus.value = 'rejected'
          hasAgentAccess.value = false
          break
        default:
          agentStatus.value = 'no_application'
          hasAgentAccess.value = false
      }
    } else {
      // 没有申请记录
      agentStatus.value = 'no_application'
      hasAgentAccess.value = false
    }
  } catch (error) {
    console.error('检查中介申请状态失败:', error)
    agentStatus.value = 'no_application'
    hasAgentAccess.value = false
  } finally {
    loading.value = false
  }
}

// 跳转到实名认证页面
const goToIdentityVerification = () => {
  router.push('/user/profile')
}

// 跳转到中介申请页面
const goToAgentApply = () => {
  router.push('/agent/apply')
}

// 跳转到申请状态页面
const goToAgentStatus = () => {
  router.push('/agent/status')
}

// 刷新状态
const refreshStatus = async () => {
  await checkAgentStatus()
  ElMessage.success('状态已刷新')
}

// 页面加载时检查状态
onMounted(() => {
  checkAgentStatus()
})

// 房源统计数据
const totalHouses = ref(128)
const agentHouses = ref(24)
const favoriteHouses = ref(12)
const contactedHouses = ref(36)
const applicationCount = ref(8) // 新增的申请代理数量

// 查看所有申请代理
const goToAllApplications = () => {
  router.push('/agent/house-applications')
}

// 房源列表数据
const houseList = ref([
  {
    id: 1,
    title: '精致一居室公寓',
    address: '北京市朝阳区CBD建国路123号',
    price: 4500,
    layout: '一居室',
    area: 60,
    orientation: '南北',
    image: 'https://images.unsplash.com/photo-1560448204-e02f11c3d0e2?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1769&q=80',
    tags: ['近地铁', '精装修', '拎包入住', '新上架'],
    favorited: false
  },
  {
    id: 2,
    title: '豪华两居室',
    address: '上海市浦东新区陆家嘴456号',
    price: 8500,
    layout: '两居室',
    area: 90,
    orientation: '南北',
    image: 'https://images.unsplash.com/photo-1600585154340-be6161a56a0c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1770&q=80',
    tags: ['学区房', '地铁房', '新上架'],
    favorited: true
  },
  {
    id: 3,
    title: '舒适三居室别墅',
    address: '广州市天河区珠江新城789号',
    price: 15000,
    layout: '三居室',
    area: 150,
    orientation: '南北',
    image: 'https://images.unsplash.com/photo-1600596542815-ffad4c1539a9?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1770&q=80',
    tags: ['别墅', '带花园', '独栋'],
    favorited: false
  },
  {
    id: 4,
    title: '现代化loft公寓',
    address: '深圳市南山区科技园101号',
    price: 6500,
    layout: '一居室',
    area: 75,
    orientation: '东西',
    image: 'https://images.unsplash.com/photo-1575517114892-6a0eb8c4fd5d?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1770&q=80',
    tags: ['loft', '创意空间', ' loft'],
    favorited: false
  },
  {
    id: 5,
    title: '精品复式公寓',
    address: '杭州市西湖区西湖边202号',
    price: 7200,
    layout: '两居室',
    area: 85,
    orientation: '南北',
    image: 'https://images.unsplash.com/photo-1600607687939-ce8a6c25118c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1753&q=80',
    tags: ['复式', '江景房', '近商圈'],
    favorited: true
  },
  {
    id: 6,
    title: '温馨单间出租',
    address: '成都市锦江区春熙路303号',
    price: 2800,
    layout: '单间',
    area: 35,
    orientation: '南',
    image: 'https://images.unsplash.com/photo-1584622650111-993a426fbf0a?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1770&q=80',
    tags: ['单间', '经济实惠', '学生首选'],
    favorited: false
  }
])

// 已代理房源数据
const agencyHousesData = ref([
  {
    id: 7,
    title: '高端服务式公寓',
    address: '北京市朝阳区三里屯404号',
    price: 12000,
    layout: '两居室',
    area: 120,
    orientation: '南北',
    image: 'https://images.unsplash.com/photo-1600607687920-4e2a09cf159d?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1769&q=80',
    tags: ['服务式公寓', '高端', '配套设施齐全'],
    favorited: false
  },
  {
    id: 8,
    title: '市中心精装公寓',
    address: '上海市黄浦区南京路505号',
    price: 9500,
    layout: '一居室',
    area: 70,
    orientation: '东西',
    image: 'https://images.unsplash.com/photo-1600596542815-ffad4c1539a9?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1770&q=80',
    tags: ['市中心', '交通便利', '精装'],
    favorited: true
  },
  {
    id: 9,
    title: '学区房出租',
    address: '北京市海淀区中关村一小附近606号',
    price: 11000,
    layout: '三室一厅',
    area: 100,
    orientation: '南北',
    image: 'https://images.unsplash.com/photo-1600566753157-7b40d354b951?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1770&q=80',
    tags: ['学区房', '优质教育资源', '安静'],
    favorited: false
  }
])

// 已收藏房源数据
const favoritedHousesData = ref([
  {
    id: 2,
    title: '豪华两居室',
    address: '上海市浦东新区陆家嘴456号',
    price: 8500,
    layout: '两居室',
    area: 90,
    orientation: '南北',
    image: 'https://images.unsplash.com/photo-1600585154340-be6161a56a0c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1770&q=80',
    tags: ['学区房', '地铁房', '新上架'],
    favorited: true
  },
  {
    id: 5,
    title: '精品复式公寓',
    address: '杭州市西湖区西湖边202号',
    price: 7200,
    layout: '两居室',
    area: 85,
    orientation: '南北',
    image: 'https://images.unsplash.com/photo-1600607687939-ce8a6c25118c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1753&q=80',
    tags: ['复式', '江景房', '近商圈'],
    favorited: true
  }
])

// 根据标签获取标签类型
const getTagType = (tag) => {
  const tagTypes = {
    '近地铁': 'success',
    '精装修': 'warning',
    '拎包入住': 'info',
    '新上架': 'danger',
    '学区房': 'primary',
    '地铁房': 'success',
    '别墅': 'primary',
    '带花园': 'success',
    '独栋': 'warning',
    'loft': 'info',
    '创意空间': 'danger',
    '复式': 'primary',
    '江景房': 'success',
    '近商圈': 'warning',
    '单间': 'info',
    '经济实惠': 'danger',
    '学生首选': 'primary',
    '服务式公寓': 'success',
    '高端': 'warning',
    '配套设施齐全': 'info',
    '市中心': 'danger',
    '交通便利': 'primary',
    '精装': 'success',
    '优质教育资源': 'warning',
    '安静': 'info',
    '海景房': 'primary',
    '豪华装修': 'success',
    '高端社区': 'warning',
    '商务区': 'info'
  }
  return tagTypes[tag] || 'info'
}

// 顶部导航栏跳转
const goToHome = () => {
  router.push('/')
}

const goToSearch = () => {
  router.push('/house/search')
}

const goToPublish = () => {
  router.push('/house/publish')
}

const goToMessages = () => {
  router.push('/message')
}

// 用户命令处理
const handleUserCommand = (command) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      router.push('/settings')
      break
    case 'logout':
      ElMessage.success('退出登录成功')
      localStorage.removeItem('userEmail')
      localStorage.removeItem('isLoggedIn')
      router.push('/login')
      break
  }
}

// 侧边栏跳转
const goToPublishNew = () => {
  router.push('/house/publish')
}

const goToMyHouses = () => {
  router.push('/house/my-houses')
}

const goToAgentHouses = () => {
  router.push('/agent/houses')
}

const goToTenant = () => {
  router.push('/rental/tenant')
}

const goToLandlord = () => {
  router.push('/rental/landlord')
}

const goToAgentRental = () => {
  router.push('/rental/agent')
}

const goToProfile = () => {
  router.push('/profile')
}

// 菜单选择处理
const handleSelect = (key) => {
  switch (key) {
    case '1':
      router.push('/')
      break
    case '2':
      router.push('/house/search')
      break
    case '3':
      router.push('/house/publish')
      break
    case '4':
      router.push('/message')
      break
  }
}

// 搜索房源
const searchHouses = () => {
  ElMessage.info(`搜索关键词: ${searchKeyword.value}`)
}

// 重置筛选
const resetFilters = () => {
  searchKeyword.value = ''
  selectedCity.value = ''
  selectedPriceRange.value = ''
  selectedLayout.value = ''
  ElMessage.info('筛选条件已重置')
}

// 查看房源详情
const viewHouseDetail = (id) => {
  // 通过路由传递参数，标识用户类型为中介
  router.push(`/agent/house-detail/${id}`)
}

// 切换收藏状态
const toggleFavorite = (house) => {
  house.favorited = !house.favorited
  ElMessage.success(house.favorited ? '已收藏' : '已取消收藏')
}

// 申请代理
const applyForAgency = (house) => {
  ElMessageBox.confirm(
      `确定要申请代理"${house.title}"吗？`,
      '申请代理',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    // 模拟申请代理的API调用
    setTimeout(() => {
      ElMessage.success('申请已提交，请等待审核')
    }, 1000)
  }).catch(() => {
    // 用户取消操作
  })
}

// 联系房东
const contactLandlord = (house) => {
  ElMessage.success(`正在联系"${house.title}"的房东`)
}

// 查看已收藏房源
const favoritedHouses = computed(() => {
  return houseList.value.filter(house => house.favorited)
})

// 查看已代理房源
const agencyHouses = computed(() => {
  return agencyHousesData.value
})

// 处理标签页切换
const handleTabChange = (tab) => {
  activeTab.value = tab
  ElMessage.info(`切换到${tab === 'all' ? '全部房源' : tab === 'favorited' ? '已收藏' : '已代理'}标签页`)
}

// 分页相关方法
const handleSizeChange = (val) => {
  pageSize.value = val
  ElMessage.info(`每页显示 ${val} 条`)
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  ElMessage.info(`当前页: ${val}`)
}

// 查看代理详情
const viewAgencyDetails = (house) => {
  ElMessage.success(`查看"${house.title}"的代理详情`)
}

// 管理房源
const manageHouse = (house) => {
  ElMessage.success(`管理"${house.title}"房源`)
}
</script>

<style scoped>
.agent-houses-container {
  display: flex;
  min-height: 100vh;
  overflow: hidden;
  position: relative;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;
}

/* 精美的动态背景 */
.dynamic-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, #1a2980, #26d0ce);
  overflow: hidden;
}

/* 建筑物 */
.building {
  position: absolute;
  bottom: 0;
  background: rgba(30, 30, 50, 0.8);
  border-radius: 5px 5px 0 0;
  box-shadow: 0 -5px 15px rgba(0, 0, 0, 0.3);
}

.building-1 {
  left: 10%;
  width: 80px;
  height: 300px;
  animation: buildingGlow 4s infinite alternate;
}

.building-2 {
  left: 25%;
  width: 120px;
  height: 400px;
  animation: buildingGlow 5s infinite alternate-reverse;
}

.building-3 {
  left: 45%;
  width: 100px;
  height: 350px;
  animation: buildingGlow 6s infinite alternate;
}

.building-4 {
  left: 65%;
  width: 140px;
  height: 450px;
  animation: buildingGlow 4.5s infinite alternate-reverse;
}

.building-5 {
  left: 80%;
  width: 90px;
  height: 320px;
  animation: buildingGlow 5.5s infinite alternate;
}

@keyframes buildingGlow {
  0% {
    box-shadow: 0 -5px 15px rgba(0, 0, 0, 0.3);
  }
  100% {
    box-shadow: 0 -5px 25px rgba(100, 200, 255, 0.6);
  }
}

/* 云朵 */
.cloud {
  position: absolute;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 50%;
  filter: blur(2px);
}

.cloud-1 {
  top: 20%;
  left: 5%;
  width: 80px;
  height: 30px;
  animation: cloudMove 25s linear infinite;
}

.cloud-2 {
  top: 35%;
  left: 70%;
  width: 120px;
  height: 40px;
  animation: cloudMove 30s linear infinite;
}

.cloud-3 {
  top: 15%;
  left: 40%;
  width: 100px;
  height: 35px;
  animation: cloudMove 35s linear infinite;
}

@keyframes cloudMove {
  0% {
    transform: translateX(-100px);
  }
  100% {
    transform: translateX(calc(100vw + 100px));
  }
}

/* 太阳 */
.sun {
  position: absolute;
  top: 80px;
  right: 100px;
  width: 80px;
  height: 80px;
  background: radial-gradient(circle, #ffeb3b, #ffc107);
  border-radius: 50%;
  box-shadow: 0 0 40px #ff9800, 0 0 80px #ff5722;
  animation: sunPulse 3s infinite alternate;
}

@keyframes sunPulse {
  0% {
    transform: scale(1);
    box-shadow: 0 0 40px #ff9800, 0 0 80px #ff5722;
  }
  100% {
    transform: scale(1.1);
    box-shadow: 0 0 60px #ff9800, 0 0 100px #ff5722;
  }
}

/* 光线 */
.light-ray {
  position: absolute;
  top: 120px;
  right: 140px;
  width: 400px;
  height: 200px;
  background: linear-gradient(to right, transparent, rgba(255, 235, 59, 0.2), transparent);
  transform-origin: left center;
  animation: rayRotate 10s infinite linear;
}

.ray-1 {
  transform: rotate(0deg);
  animation-delay: 0s;
}

.ray-2 {
  transform: rotate(60deg);
  animation-delay: 2s;
}

.ray-3 {
  transform: rotate(120deg);
  animation-delay: 4s;
}

@keyframes rayRotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

/* 浮动装饰元素 */
.floating-shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 15s infinite linear;
}

.shape-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  left: -100px;
  animation-duration: 20s;
  background: radial-gradient(circle, rgba(74, 144, 226, 0.2) 0%, transparent 70%);
}

.shape-2 {
  width: 200px;
  height: 200px;
  bottom: -80px;
  right: -80px;
  animation-duration: 25s;
  animation-direction: reverse;
  background: radial-gradient(circle, rgba(255, 107, 107, 0.2) 0%, transparent 70%);
}

.shape-3 {
  width: 150px;
  height: 150px;
  top: 30%;
  right: 20%;
  animation-duration: 15s;
  background: radial-gradient(circle, rgba(80, 227, 194, 0.2) 0%, transparent 70%);
}

.shape-4 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 20%;
  animation-duration: 18s;
  animation-direction: reverse;
  background: radial-gradient(circle, rgba(255, 209, 102, 0.2) 0%, transparent 70%);
}

.shape-5 {
  width: 250px;
  height: 250px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation-duration: 22s;
  background: radial-gradient(circle, rgba(155, 89, 182, 0.2) 0%, transparent 70%);
}

.shape-6 {
  width: 180px;
  height: 180px;
  top: 70%;
  left: 10%;
  animation-duration: 28s;
  background: radial-gradient(circle, rgba(231, 76, 60, 0.2) 0%, transparent 70%);
}

@keyframes float {
  0% {
    transform: translate(0, 0) rotate(0deg);
  }
  25% {
    transform: translate(20px, 20px) rotate(90deg);
  }
  50% {
    transform: translate(0, 40px) rotate(180deg);
  }
  75% {
    transform: translate(-20px, 20px) rotate(270deg);
  }
  100% {
    transform: translate(0, 0) rotate(360deg);
  }
}

.agent-houses-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  z-index: 1;
  position: relative;
}

.top-navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 30px;
  background: rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.app-title {
  color: white;
  font-size: 24px;
  font-weight: 700;
  margin: 0;
}

.navbar-center {
  flex: 1;
  margin: 0 20px;
}

:deep(.el-menu--horizontal) {
  border: none;
  background: transparent !important;
}

:deep(.el-menu--horizontal .el-menu-item) {
  color: white;
  border: none;
}

:deep(.el-menu--horizontal .el-menu-item:hover) {
  background: rgba(255, 255, 255, 0.2) !important;
}

:deep(.el-menu--horizontal .el-menu-item.is-active) {
  color: #4A90E2 !important;
  border: none;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  color: white;
}

.username {
  font-size: 16px;
  font-weight: 500;
}

.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.sidebar {
  width: 250px;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  padding: 20px 0;
  height: 100%;
  box-shadow: 3px 0 10px rgba(0, 0, 0, 0.2);
}

.el-menu-vertical {
  border: none !important;
  height: 100%;
}

.el-menu-item,
.el-sub-menu__title {
  margin: 5px 10px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.el-menu-item:hover,
.el-sub-menu__title:hover {
  background-color: rgba(255, 255, 255, 0.1) !important;
}

.agent-houses-content {
  height: 100%;
  overflow-y: auto;
  padding: 20px;
}

.agent-houses-content::-webkit-scrollbar {
  width: 8px;
}

.agent-houses-content::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 4px;
}

.agent-houses-content::-webkit-scrollbar-thumb {
  background: rgba(74, 144, 226, 0.6);
  border-radius: 4px;
}

.agent-houses-content::-webkit-scrollbar-thumb:hover {
  background: rgba(74, 144, 226, 0.8);
}

.houses-header {
  margin-bottom: 30px;
}

.houses-header h2 {
  color: #2c3e50;
  font-size: 28px;
  margin: 0 0 10px;
}

.houses-header p {
  color: #7f8c8d;
}

.houses-stats {
  margin-bottom: 30px;
}

.stat-card {
  display: flex;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.stat-icon {
  width: 70px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
}

.stat-content {
  flex: 1;
  padding: 15px;
}

.stat-value {
  font-size: 24px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 5px;
}

.stat-title {
  font-size: 14px;
  color: #7f8c8d;
}

.bg-blue {
  background: linear-gradient(135deg, #4A90E2 0%, #6b93d8 100%);
}

.bg-green {
  background: linear-gradient(135deg, #7ED321 0%, #8bc34a 100%);
}

.bg-orange {
  background: linear-gradient(135deg, #FFA500 0%, #ffb74d 100%);
}

.bg-red {
  background: linear-gradient(135deg, #FF6B6B 0%, #ff8a80 100%);
}

.bg-purple {
  background: linear-gradient(135deg, #9B59B6 0%, #ba68c8 100%);
}

.houses-container {
  padding: 20px 0;
}

.house-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s ease;
  height: 100%;
}

.house-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.12);
}

.house-image {
  position: relative;
  height: 200px;
}

.house-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.house-card:hover .house-image img {
  transform: scale(1.05);
}

.house-price {
  position: absolute;
  top: 15px;
  right: 15px;
  background: rgba(74, 144, 226, 0.9);
  color: white;
  padding: 5px 10px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 14px;
}

.favorite-button {
  position: absolute;
  top: 15px;
  left: 15px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.favorite-button:hover {
  background: rgba(255, 255, 255, 1);
}

.favorite-button.favorited {
  color: #ff6b6b;
}

.agency-badge {
  position: absolute;
  top: 15px;
  left: 15px;
  background: #50E3C2;
  color: white;
  padding: 5px 10px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 12px;
}

.house-info {
  padding: 20px;
}

.house-title {
  margin: 0 0 10px;
  font-size: 18px;
  font-weight: 700;
  color: #2c3e50;
}

.house-address {
  display: flex;
  align-items: center;
  gap: 5px;
  margin: 0 0 15px;
  font-size: 14px;
  color: #7f8c8d;
}

.house-meta {
  display: flex;
  gap: 15px;
  margin-bottom: 15px;
  font-size: 13px;
  color: #7f8c8d;
}

.house-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 20px;
}

.house-actions {
  display: flex;
  gap: 10px;
}

.pagination-container {
  display: flex;
  justify-content: center;
  padding: 20px 0;
}

/* 无权限访问样式 */
.no-access-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
  padding: 40px 20px;
}

.no-access-card {
  background: white;
  border-radius: 20px;
  padding: 60px 40px;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
  max-width: 600px;
  width: 100%;
}

.no-access-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  color: white;
}

.no-access-card h2 {
  color: #2c3e50;
  font-size: 28px;
  margin: 0 0 15px;
  font-weight: 700;
}

.no-access-card p {
  color: #7f8c8d;
  font-size: 16px;
  margin: 0 0 30px;
  line-height: 1.6;
}

.no-access-actions {
  display: flex;
  gap: 15px;
  justify-content: center;
  margin-bottom: 40px;
  flex-wrap: wrap;
}

.no-access-tips {
  background: #f8f9fa;
  border-radius: 12px;
  padding: 25px;
  text-align: left;
}

.no-access-tips h3 {
  color: #2c3e50;
  font-size: 18px;
  margin: 0 0 15px;
  font-weight: 600;
}

.no-access-tips ol {
  margin: 0;
  padding-left: 20px;
  color: #5a6c7d;
}

.no-access-tips li {
  margin-bottom: 8px;
  line-height: 1.5;
}

@media (max-width: 992px) {
  .sidebar {
    width: 80px;
  }

  .app-title,
  .el-menu-item span,
  .el-sub-menu__title span {
    display: none;
  }

  .filter-section .el-row {
    flex-direction: column;
    gap: 15px;
  }

  .filter-section .el-col {
    width: 100%;
  }

  .houses-stats .el-row {
    flex-direction: column;
    gap: 15px;
  }

  .houses-stats .el-col {
    width: 100%;
  }

  .houses-container .el-row {
    flex-direction: column;
    gap: 15px;
  }

  .houses-container .el-col {
    width: 100%;
  }

  .no-access-card {
    padding: 40px 20px;
  }

  .no-access-actions {
    flex-direction: column;
    align-items: center;
  }

  .no-access-actions .el-button {
    width: 200px;
  }
}
</style>
